import { setToken, getToken, removeToken } from '@/utils/auth'
import { login, getUserInfo } from '@/api/user'
import { resetRouter } from '@/router'
const state = {
  token: getToken() || null,
  userInfo: {}
}
const mutations = {
  setToken(state, newToken) {
    state.token = newToken
    setToken(newToken)
  },
  updateUserInfo(state, userInfo) {
    state.userInfo = userInfo
  }
}
const actions = {
  async login(context, payload) {
    const res = await login(payload)
    console.log(res)
    const token = res.data
    context.commit('setToken', token)
  },
  async getProFile(context) {
    const res = await getUserInfo()
    console.log(res)
    context.commit('updateUserInfo', res.data)
    console.log(state.userInfo)
    return res.data
  },
  logout(context) {
    // state.token = null
    removeToken()
    state.userInfo = {}
    resetRouter()
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
